﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml;
using bmapi.HelperClass;
using bmapi.Models.CADModel;
using bmapi.Models.DataEntity;
using bmapi.SQLServerAccess;

namespace bmapi.Controllers
{
    [RoutePrefix("cadLogin")]
    public class CADLoginController : CADController
    {
        [HttpPost, Route("Login")]
        public string Login(dynamic json)
        {
            WriteDebug("Login","1");
            if (json == null)
                return ReturnError("没有登陆信息");
            else
            {
                WriteDebug("Login", "2");
                string loginid = json.login_id.ToString();
                string password = json.password.ToString();
                SQLCommand sc = new SQLCommand("select * from User_tb where login_id=@loginid");
                sc.Add("@loginid", loginid);
                DataTable dt = sc.ExecuteScalars();
                List<User> loginuser = AssistFun.DataTableToList<User>(dt);
                WriteDebug("Login", "3");
                if (loginuser.Count == 1)
                {
                    if (loginuser[0].password == password.Trim())
                    {
                        //生成token
                        WriteDebug("Login", "4");
                        string token = CreateToken(loginuser[0].login_id);

                        return ReturnXml("成功", token, loginuser[0]);
                    }
                    else
                        return ReturnError("输入密码有误");
                }
                else
                {
                    if (loginuser.Count > 1)
                        return ReturnError("该账号存在多个，登录无效");
                    else
                        return ReturnError("找不到当前用户");
                }
            }
        }
        /// <summary>
        /// 成功登陆后返回cad的xml数据
        /// </summary>
        /// <param name="user"></param>
        /// <param name="tip"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private string ReturnXml(string tip, string token, User user)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateElement("login");
            doc.AppendChild(root);

            XmlElement p = doc.CreateElement("status");
            p.InnerText = "200";
            root.AppendChild(p);

            p = doc.CreateElement("tip");
            p.InnerText = tip;
            root.AppendChild(p);

            p = doc.CreateElement("token");
            p.InnerText = token;
            root.AppendChild(p);

            p = doc.CreateElement("name");
            p.InnerText = user.name;
            root.AppendChild(p);

            p = doc.CreateElement("process");
            p.InnerText = user.code.ToString();
            root.AppendChild(p);

            //这个人,是否需要在结束任务时, 上传样式文件
            SQLCommand sc = new SQLCommand("select upload from Process_tb where code=@code and PLID=@plid");
            sc.Add("@code", user.code);
            sc.Add("@plid", user.PLID);
            string upload = sc.ExecuteScalar();

            p = doc.CreateElement("upload");
            p.InnerText = upload;
            root.AppendChild(p);

            p = doc.CreateElement("root");
            p.InnerText = m_buyiRoot;
            root.AppendChild(p);

            sc = new SQLCommand("SELECT key_val FROM System_tb where key_name=@name", "@name", "FtpPassword");
            string keyval = sc.ExecuteScalar();
            p = doc.CreateElement("passwod");
            if (keyval != null)
                p.InnerText = keyval;
            else
                p.InnerText = "By2018";
            root.AppendChild(p);

            return ConvertXmlToString(doc);
        }

        private string ReturnError(string tip)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateElement("login");
            doc.AppendChild(root);

            XmlElement p = doc.CreateElement("status");
            p.InnerText = "201";
            root.AppendChild(p);

            p = doc.CreateElement("tip");
            p.InnerText = tip;
            root.AppendChild(p);

            return ConvertXmlToString(doc);
        }
    }
}
